package org.zoomdev.zoom.common.caster;

import org.zoomdev.zoom.common.utils.Classes;

class WrapExceptionCaster implements ValueCaster {

    protected ValueCaster valueCaster;

    public WrapExceptionCaster(ValueCaster valueCaster) {
        this.valueCaster = valueCaster;
    }

    @Override
    public Object to(Object src) {
        try {
            return valueCaster.to(src);
        } catch (Throwable t) {
            t = Classes.getCause(t);
            if (t instanceof CasterException) {
                throw (CasterException) t;
            }
            throw new CasterException(t);
        }

    }
}
